#include "head.h"
int net_mkdir(request_t * request, int net_fd, MYSQL *mysql){
    

    //printf("进入到net_mkdir\n");
    // 初始化响应报文
    response_t response;
    memset(&response, 0, sizeof(response));

    char user_name[60];
    strcpy(user_name, request->user_name);
    // 服务端接收的就是/test   -> 这个是客户端认为自己在的路径
    char path[1024];
    //strcpy(path, request->user_name);
    //strcpy(path, "/");
    strcpy(path, request->path);
    printf("服务端拿到的path: %s\n",path);

    char args[60];
    strcpy(args, request->args);
    // 再拼接为/test/code  -> 这个是要insert到表中的path
    char full_path[4096] = {0};
    // 拼接当前路径
    strcpy(full_path, path);

    if(strcmp(full_path, "/") == 0){
        // 如果当前是根目录的话就不用拼接"/"
        strcat(full_path, args);
    }else{
        strcat(full_path, "/");
        strcat(full_path, args);
    }

    printf("要插入的path为: %s\n",full_path);
    // 执行sql语句,插入一条记录

    char query[2048] = {0};
    printf("mkdir: args: %s\n",request->args);
    printf("mkdir: user_name: %s\n",request->user_name);
    printf("mkdir: path: %s\n",path);
    printf("mkdir: full_path: %s\n",full_path);

    snprintf(query, sizeof(query), 
    "insert into files values \
    (null, '%s', (select id from users where username = '%s'), \
    (select f.id from files f inner join users u on f.user_id = u.id \
    where f.path = '%s' and f.user_id = \
    (select u.id from users u where u.id = f.user_id and u.username = '%s')), \
    '%s', 0, null, now(), 1, 0, 1)", \
        args, 
        user_name, 
        path, 
        user_name,
        full_path);
    printf("mkdir: %s\n",query);    
    int ret = mysql_query(mysql, query);

    printf("mysql_query: %s\n",mysql_error(mysql));

   

    if(ret != 0){
        strcpy(response.args, "文件夹创建失败");
        send(net_fd, &response, sizeof(response), 0);
        //printf("mkdir: 已发送mkdir响应报文\n");
        return 0;
    }
    strcpy(response.args, "文件夹创建成功");
    // 发送响应报文
    send(net_fd, &response, sizeof(response), 0);
    //printf("mkdir: 已发送mkdir响应报文\n");

    return 0;
}
